<?php

class Ead_ContatoprfController extends Ead_Controller_Action
{

    public function init()
    {
       
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/contatoprf.js');

        $sessao = new Zend_Session_Namespace('Professor');
        $this->view->professor = $sessao->professor;
        
        $query = 'SELECT SQL_CACHE CBS_ID, CBS_NOM FROM CURSOBASE WHERE CBS_STA = 1 ORDER BY CBS_ORD ASC';
        $result = $this->db->fetchAll($query);
        $this->view->cursos = $result;
        
        //$query = 'SELECT SQL_CACHE * FROM CURSO WHERE CUR_STA = 1';
        $query = 'SELECT SQL_CACHE c.CUR_ID FROM CURSO c, NUMEROTURMA t  WHERE c.CUR_TRM = t.NRT_ID AND c.CUR_STA = 1 and c.CUR_ANO >= "'.date('Y').'" ORDER BY t.NRT_SEM ASC, t.NRT_NOM ASC, c.CUR_NOM';
        $result = $this->db->fetchAll($query);
        $this->view->turmas = $result;
        
        
        if($this->getRequest()->isPost()) {
            
            try {
                
                $this->db->beginTransaction();
                
                $formData = $this->getRequest()->getPost();

                $arr = array(
                    'MRC_DAT' => date('Y-m-d'),
                    'MRC_HOR' => date('H:i:00'),
                    'MRC_ASS' => $formData['assunto'],
                    'MRC_TXT' => $formData['mensagem'],
                    'USU_ID'  => Zend_Auth::getInstance()->getStorage()->read()->USU_ID,
                    'ALU_ID'  => '',
                    'CBS_ID'  => '',
                    'CUR_ID'  => $formData['turma'],
                    'MRC_STA' => '1', 
                );
                $this->db->insert('MURAL_RECADOS',$arr);
                $id = $this->db->lastInsertId();

                if(isset($formData['curso']) && $formData['curso'] > 0){
                    $query = 'SELECT SQL_CACHE DISTINCT m.ALU_ID '
                            . 'FROM MATRICULA m, CURSO c, CURSOBASE cb '
                            . 'WHERE m.CUR_ID = c.CUR_ID AND cb.CBS_ID = c.CBS_ID '
                            . 'AND c.CUR_STA = 1 AND c.CBS_ID = '.$formData['curso'].'';
                }
                else if(isset($formData['turma']) && $formData['turma'] > 0){
                    $query = 'SELECT SQL_CACHE DISTINCT m.ALU_ID '
                            . 'FROM MATRICULA m, CURSO c, CURSOBASE cb '
                            . 'WHERE m.CUR_ID = c.CUR_ID AND cb.CBS_ID = c.CBS_ID '
                            . 'AND c.CUR_STA = 1 AND c.CUR_ID = '.$formData['turma'].'';

                }    

                $result = $this->db->fetchAll($query);
                foreach($result as $alu){

                    $query = 'SELECT SQL_CACHE USU_ID FROM ALUNO WHERE ALU_ID = '.$alu['ALU_ID'];
                    $row = $this->db->fetchRow($query);

                    $arr2 = array(
                        'MRC_ID'  => $id,
                        'USU_ID'  => $row['USU_ID'],
                        'ALU_ID'  => $alu['ALU_ID'],
                        'MRM_DAT' => $arr['MRC_DAT'],
                        'MRM_HOR' => $arr['MRC_HOR'],
                        'MRM_ASS' => $arr['MRC_ASS'],
                        'MRM_TXT' => $arr['MRC_TXT'],
                        'MRM_STA' => '1',
                    );
                    
                    $this->db->insert('MURAL_RECADOS_MSG',$arr2);
                }
                
                $this->db->commit();
                
                echo '<script>alert("Mensagem enviada com sucesso!");</script>';
                
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->db->rollBack();
            }
        }
    }
    
    
}